package com.bac.rentmap.web.servlet;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class AcceptHeaderViewResolver extends InternalResourceViewResolver {
    public static final String HEADER_NAME = "Accept";
    private String headerValue;

    public View resolveViewName(String viewName, Locale locale) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
        String requestHeader = request.getHeader(HEADER_NAME);
        if(requestHeader != null && requestHeader.contains(headerValue)){
            return super.resolveViewName(viewName, locale);
        }
        return null;
    }

    @Required
    public void setHeaderValue(String headerValue) {
        this.headerValue = headerValue;
    }
}
